<template>
	<view class="content">
		<view class="login-bg">
			<view class="login-card">
				<view class="login-head">请输入你的账户</view>
				<view class="login-input login-margin-b">
					<input v-model="loginPhone" type="number" placeholder="手机号" maxlength="11"/>
				</view>
				<view class="login-input">
					<input v-model="loginPassword" type="password" placeholder="请输入密码(8-16位)" />
				</view>
				<view style="padding: 0 3%;">
					<text style="color: red;">{{message}}</text>
				</view>
				<view class="login-function">
					<view class="login-register" @click="go_register">快速注册></view>
				</view>
			</view>
		</view>
		<view class="login-btn">
			<button class="landing" type="primary" @click="goLogin">登录</button>
		</view>
	</view>
</template>

<script>
	import { login } from '@/common/api'
	export default {
		data() {
			return {
				loginPhone: '',
				loginPassword: '',
				message: '',
				loading:false,
			};
		},
		onLoad() {
		
		},
		methods: {
			goLogin() {
				let phone_number = this.loginPhone;
				let password = this.loginPassword;
				let hash = 'acbcefg';
				let timestamp = Math.round(new Date().getTime() / 1000).toString();
				if (!(/^1(3|4|5|7|8|9)\d{9}$/.test(phone_number))) {
					this.message = "手机号码有误，请重填";
					return false;
				}
				if (!password) {
					this.message = "密码为空";
					return false;
				}
				uni.showLoading({
					title: '登录中。。。',
					mask: false
				});
				
				login({phone_number:phone_number,password:password,hash:hash,timestamp:timestamp}).then(res=>{
					
					if(res.code == '200'){
						uni.hideLoading();
						uni.showToast({
							title: '登录成功',//标题 必填
							icon: 'none',//图标
						    mask: false,//是否显示透明蒙层，防止触摸穿透，默认：false
						    duration: 1000,//提示的延迟时间，单位毫秒，默认：1500
						});
						//用户信息
						uni.setStorageSync('userInfo',JSON.stringify(res.data.user_info));
						//公式信息
						let formula = res.data.private_formula;
						formula.sort(function(a, b) {
						    return a.updated_at > b.updated_at ? -1 : 1;
						});
						uni.setStorageSync('formula',JSON.stringify(formula));
						//公司信息
						uni.setStorageSync('companyInfo',JSON.stringify(res.data.company_info));
						uni.$emit('indexloading', 12)
						setTimeout(function () {
							uni.switchTab({
								url: '../index/index'
							});
						}, 1000);
					}else{
						uni.hideLoading();
						uni.showToast({
							title: res.msg,//标题 必填
							icon: 'none',//图标
						    mask: false,//是否显示透明蒙层，防止触摸穿透，默认：false
						    duration: 2000,//提示的延迟时间，单位毫秒，默认：1500
						});
					}
					
				}).catch(
					(response) => {
						uni.hideLoading();
						uni.showToast({
							title: "系统错误",//标题 必填
							icon: 'none',//图标
						    mask: false,//是否显示透明蒙层，防止触摸穿透，默认：false
						    duration: 2000,//提示的延迟时间，单位毫秒，默认：1500
						});
						
					}
				)
			},
			go_register(){
				uni.navigateTo({
					url: './register'
				})
			}
			
		}
	}
</script>

<style>
	.landing{
		height: 84upx;
		line-height: 84upx;
		border-radius: 44upx;
		font-size: 32upx;
		background: linear-gradient(left,#FF978D, #FFBB69);
	}
	.login-btn{
		padding: 10upx 20upx;
		margin-top: 350upx;
	}
	.login-function{
		overflow: auto;
		padding: 20upx 20upx 30upx 20upx;
	}
	.login-forget{
		float: left;
		font-size: 26upx;
		color: #999;
	}
	.login-register{
		color: #666;
		float: right;
		font-size: 26upx;

	}
	.login-input input{
		background: #F2F5F6;
		font-size: 28upx;
		padding: 10upx 25upx;
		height: 62upx;
		line-height: 62upx;
		border-radius: 8upx;
	}
	.login-margin-b{
		margin-bottom: 25upx;
	}
	.login-input{
		padding: 10upx 20upx;
	}
	.login-head{
		font-size: 34upx;
		text-align: center;
		padding: 25upx 10upx 55upx 10upx;
	}
	.login-card{
		background: #fff;
		border-radius: 12upx;
		padding: 10upx 25upx;
		box-shadow: 0 6upx 18upx rgba(0,0,0,0.12);
		position: relative;
		margin-top: 120upx;
	}
	.login-bg {
		height: 260upx;
		padding: 25upx;
		background: linear-gradient(#FF978D, #FFBB69);
	}
</style>
